package cn.swing.learning;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.*;
import java.util.function.ToLongBiFunction;

import javax.swing.*;
import javax.swing.border.Border;

public class MineState extends JPanel {

	private MineFrame mineFrame;

	private JLabel[] jlabels;

	private Timer timer;

	private int index =0;

	public MineState(MineFrame mineFrame) {
		this.mineFrame = mineFrame;

		timer =  new Timer(1000, new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				setTime(index++);
			}
		});

		Border borderIn = BorderFactory.createLoweredBevelBorder();
		Border borderOut = BorderFactory.createEmptyBorder(5, 5, 5, 5);
		Border border = BorderFactory.createCompoundBorder(borderOut, borderIn);
		this.setBorder(border);

		this.setBackground(Color.LIGHT_GRAY);

		this.setLayout(new BorderLayout());
		jlabels = new JLabel[7];
		Box box = Box.createHorizontalBox();
		for (int i = 0; i < jlabels.length; i++) {
			jlabels[i] = new JLabel();
			jlabels[i].setIcon(Tools.ds[8]);

		}
		box.add(Box.createHorizontalStrut(5));

		box.add(jlabels[0]);
		box.add(jlabels[1]);
		box.add(jlabels[2]);

		box.add(Box.createHorizontalGlue());
		box.add(jlabels[3]);
		box.add(Box.createHorizontalGlue());
		box.add(jlabels[4]);
		box.add(jlabels[5]);
		box.add(jlabels[6]);
		box.add(Box.createHorizontalStrut(5));
		jlabels[3].setIcon(new ImageIcon("./picture/face0.gif"));

		jlabels[3].addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				jlabels[3].setIcon(new ImageIcon("./picture/face1.gif"));
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				jlabels[3].setIcon(new ImageIcon("./picture/face0.gif"));
			}
		});


		this.add(box);
	}

	public void setTime(int times){
		jlabels[4].setIcon(new ImageIcon("./picture/d"+(times/100%10)+".gif"));
		jlabels[5].setIcon(new ImageIcon("./picture/d"+(times/10%10)+".gif"));
		jlabels[6].setIcon(new ImageIcon("./picture/d"+(times%10)+".gif"));
	}

	public MineFrame getMineFrame() {
		return mineFrame;
	}

	public JLabel[] getJlabels() {
		return jlabels;
	}

	public Timer getTimer() {
		return timer;
	}
}
